from rest_framework.generics import ListAPIView,RetrieveAPIView
from rest_framework.views import APIView
from apps.recruit.models import Enterprise
from apps.recruit.serializers.EnterpriseDetailSerializer import EnterpriseDetailModelSerializer
from apps.recruit.serializers.EnterpriseSerializer import EnterpriseModelSerializer
from rest_framework.response import Response

# 热门企业展示
class HotEnterpriseAPIView(ListAPIView):
    queryset = Enterprise.objects.all()
    serializer_class = EnterpriseModelSerializer


# 企业详情
class EnterpriseDetailAPIView(RetrieveAPIView):
    queryset = Enterprise.objects.all()
    serializer_class = EnterpriseDetailModelSerializer


# 企业访问量显示
class EnterpriseVisitView(APIView):
    def put(self, request, pk):
        enterprise = Enterprise.objects.get(id=pk)
        enterprise.visits += 1
        enterprise.save()

        return Response({"message": "更新成功", "success": True })


# 企业收藏  ==> 登陆状态
class CollectEnterpriseView(APIView):
    def post(self, request, pk):
        user = request.user
        enterprise = Enterprise.objects.get(id=pk)
        users = enterprise.users.all()
        if user not in users:
            enterprise.users.add(user)
            return Response(data={"message": "收藏成功", "success": True})


# 取消收藏 ==> 登录状态
class CancelCollectEnterpriseView(APIView):
    def post(self,request,pk):
        user = request.user
        enterprise = Enterprise.objects.get(id=pk)
        users =enterprise.users.all()
        if user in users:
            enterprise.users.remove(user)
            return Response(data={"message": "成功取消收藏", "success": True})



# 路由 - 视图
HotEnterprise = HotEnterpriseAPIView.as_view()
EnterpriseDetail = EnterpriseDetailAPIView.as_view()
EnterpriseVisit = EnterpriseVisitView.as_view()
EnterpriseCollect = CollectEnterpriseView.as_view()
EnterpriseCancelCollect = CancelCollectEnterpriseView.as_view()